import { log, send_wx_text_toUser, show_trace, isRepeatMsg } from "./logger.js";
import { getAnswer } from "./gpt.js";
interface wxMessage {
    talker: string;
    content: string;
    type: number;
    createTime: number;
    imgPath: string;
    isSend: number;
    msgId: number;
    status: number;
    sendByUserName?: string;
}

// 点击语音按钮
// Java.perform(function () {
//     let ChattingUI = Java.use("com.tencent.mm.ui.chatting.ChattingUI");
//     ChattingUI["O7"].implementation = function (z1Var: any, bVar: any) {
//         console.log(`ChattingUI.O7 is called: z1Var=${z1Var}, bVar=${bVar}`);
//         let result = this["O7"](z1Var, bVar);
//         console.log(`ChattingUI.O7 result=${result}`);
//         return result;
//     };
// });

let gptRoleMap = new Map();

Java.perform(function () {
    var SQLiteDatabase = Java.use("com.tencent.wcdb.database.SQLiteDatabase");
    SQLiteDatabase.insertWithOnConflict.implementation = function (
        a: string,
        b: string,
        c: any,
        d: number
    ) {
        var ret = this.insertWithOnConflict(a, b, c, d);

        // 遍历 ContentValues
        if (a != "message") {
            return ret;
        }
        // 微信版本 8.0.40
        // 1 为文本消息 3为图片消息 34语音消息 43视频消息  47 表情 1090519089 文件
        // 49 为分享链接（小程序、公众号都是）
        // 只处理文本消息、图片消息、表情消息、视频消息、语音消息、文件消息
        var type = c.getAsInteger("type").intValue();
        if (![1, 3, 34, 43, 47, 49, 1090519089].includes(type)) {
            return ret;
        }
        // 映射 ContentValues 到 wxMessage
        var wxMsg: wxMessage = {
            talker: c.getAsString("talker"),
            content: c.getAsString("content"),
            type: c.getAsInteger("type").intValue(),
            createTime: c.getAsLong("createTime").longValue(),
            imgPath: c.getAsString("imgPath"),
            isSend: c.getAsInteger("isSend").intValue(),
            msgId: c.getAsLong("msgId").longValue(),
            status: c.getAsInteger("status").intValue(),
            sendByUserName: "",
        };
        if (isRepeatMsg(wxMsg.msgId)) {
            return ret;
        }
        // isSend 为 1 时为发送的消息，为 0 时为接收的消息
        if (wxMsg.isSend == 1) {
            return ret;
        }
        // 处理群聊消息
        if (wxMsg.talker.indexOf("@chatroom") !== -1) {
            wxMsg.sendByUserName = wxMsg.content.split(":")[0];
            wxMsg.content = wxMsg.content.split(":")[1].trim();
        }
        log("wxMsg: " + JSON.stringify(wxMsg));
        // 当type为1时，且content为111时回复
        if (wxMsg.content == "111") {
            wxMsg.content =
                wxMsg.content +
                "\n\n[本消息由脚本自动转发]\n来自：" +
                wxMsg.talker;
            // 转发消息
            send_wx_text_toUser(wxMsg.content, "34532798585@chatroom");
        }
        // 判断是否@我
        if (wxMsg.content.indexOf("@周瑜") !== -1) {
            // 去掉@周瑜
            wxMsg.content = wxMsg.content.replace("@周瑜", "");
            // 调用gpt
            let answer = getAnswer("周瑜", wxMsg.content);
            // 转发消息
            send_wx_text_toUser(answer, "34532798585@chatroom");
        }
        return ret;
    };
});
